import 'package:flutter/material.dart';
import 'package:flutter_magic_planet/common/app_router.dart';
import 'package:flutter_magic_planet/common/constant.dart';
import 'package:flutter_magic_planet/component/cached_image.dart';
import 'package:flutter_magic_planet/model/comic.dart';
import 'package:get/get.dart';

import 'logic.dart';

class ComicListPage extends StatelessWidget {
  final ComicListLogic logic = Get.put(ComicListLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.dark,
        backgroundColor: Colors.black,
        iconTheme: IconThemeData(color: Colors.white),
        title: Text(
          logic.query ?? '',
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Obx(() {
        return Container(
          padding:
              EdgeInsets.symmetric(horizontal: paddingHorizontal, vertical: 12),
          child: logic.comics.isEmpty
              ? Center(
                  child: Text('暂无数据'),
                )
              : ListView.builder(
                  itemBuilder: (context, index) {
                    return Padding(
                      padding: EdgeInsets.symmetric(vertical: 8),
                      child: _buildItem(logic.comics[index], "0"),
                    );
                  },
                  itemCount: logic.comics.length,
                ),
        );
      }),
    );
  }

  _buildItem(Comic bean, String sourceIndex) {
    var cover = bean.cover ?? emptyImage;
    cover = cover.trim().replaceAll('%20', '');
    print('[$cover]');
    return InkWell(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Card(
            child: CachedImage(
              width: 80,
              height: 80 / 0.75,
              fit: BoxFit.cover,
              imageUrl: cover,
            ),
            clipBehavior: Clip.antiAlias,
            elevation: 5.0,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(5.0),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.all(5.0),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  bean.cartoonType ?? '',
                  style: getStyle(Colors.grey, 12.0),
                ),
                Text(bean.title ?? '',
                    style: getStyle(Colors.black, 15.0, bold: true)),
                Text('作者: ${bean.author ?? '暂无'}',
                    style: getStyle(Colors.grey, 13.0)),
                Text(bean.descs ?? '暂无简介',
                    overflow: TextOverflow.ellipsis,
                    maxLines: 3,
                    style: getStyle(Colors.grey, 13.0)),
              ],
            ),
          )
        ],
      ),
      onTap: () {
        AppRouter.toComicChapterList(bean);
      },
    );
  }

  TextStyle getStyle(Color color, double fontSize, {bool bold = false}) {
    return TextStyle(
      color: color,
      fontSize: fontSize,
      fontWeight: bold ? FontWeight.bold : FontWeight.normal,
    );
  }
}
